package com.zxw.hotel.api;

import com.github.pagehelper.PageInfo;
import com.zxw.hotel.common.Pageinfo;
import com.zxw.hotel.dao.FacilityDao;
import com.zxw.hotel.model.Facility;
import com.zxw.hotel.service.FacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/facility")
public class FacilityApi {
    private FacilityService facilityService;
    @Autowired
    public FacilityApi(FacilityService facilityService) {
        this.facilityService = facilityService;
    }
    //查询 接受分页参数 facility对象
    @GetMapping
    public Map<String,Object> getAll(@RequestParam(defaultValue = "1") Integer pageNo,
                                     @RequestParam(defaultValue = "2147483647") Integer pageSize,
                                     Facility facility) {
        if(pageNo == null || pageNo < 1) {
            pageNo = 1;
        }
        if(pageSize == null || pageSize < 1) {
            pageSize = Integer.MAX_VALUE;
        }
        Pageinfo pageinfo = new Pageinfo(pageNo,pageSize);
        List<Facility> facilitys = facilityService.getAll(pageinfo, facility);
        PageInfo<Facility> pageInfo = new PageInfo<>(facilitys);
        Map<String,Object> map=Map.of("total",pageInfo.getTotal(),"pageNo",pageInfo.getPageNum(),"pageSize",pageInfo.getPageSize());
        return Map.of("success",true,"data",facilitys,"pi",map);
    }
    //增加    接受一个Facility对象
    @PostMapping
    public ResponseEntity<Map<String, Object>> add(@RequestBody Facility facility) {
        int add = facilityService.add(facility);
        return ResponseEntity.ok().body(Map.of("success",add>0));
    }
    //修改 接受一个Facility对象
    @PutMapping
    public ResponseEntity<Map<String, Object>> update(@RequestBody Facility facility) {
        int update = facilityService.update(facility);
        return ResponseEntity.ok().body(Map.of("success",update>0));
    }
    //删除   接受一个id数组
    @DeleteMapping
    public ResponseEntity<Map<String, Object>> delete(@RequestBody Integer[] ids) {
        int delete = facilityService.delete(ids);
        return ResponseEntity.ok().body(Map.of("success",delete>0));
    }
}



























